<div class="breadcrumbs" id="breadcrumbs">
    <ul class="breadcrumb">
        <li>
            <i class="icon-home home-icon"></i>
            <a href="#">系统管理</a>
        </li>

        <li>
            <a href="#">菜单管理</a>
        </li>
    </ul>
    <div>
        <input type="hidden" id="cupage" value="1"/>
        <input type="hidden" id="total" value="1"/>
        <input type="hidden" id="formurl" value="/manager/menu"/>
        <input type="hidden" id="startTime" value=""/>
        <input type="hidden" id="endTime" value=""/>
        <input type="hidden" name="user_id" id="user_id"/>
        <input type="hidden" name="car" id="car"/>
    </div>
</div>

<div class="page-content">
    <div class="row">
        <div class="col-xs-12">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title">
                        <i class="icon-reorder"></i>
                        筛选条件
                    </h3>
                </div>
                <div class="panel-body">
                    <form class="form-horizontal">
                    <label for="name" class="col-sm-1 control-label">菜单名称:</label>
                    <div class="col-md-2">
                        <input type="text" class="form-control" id="name" placeholder="请输入查询条件">
                    </div>
                    <div class="col-md-1">
                        <input type="button" id="btn-load" class="btn btn-primary btn-pikaqiu" data-loading-text="加载中..." value="查 询"/>
                    </div>
                    </form>
                </div>
            </div>
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title">
                        <i class="icon-reorder"></i>
                        菜单管理
                    </h3>
                    <div class="btn-add-pikaqiu">
                        <button class="btn btn-xs" data-toggle="modal" data-target="#menuAddModal"><i class="icon-plus"></i> 添 加</button>
                    </div>
                </div>
                <div class="panel-body">
                    <div class="table-responsive">
                        <table id="sample-table-2" class="table table-striped table-bordered table-hover">
                            <thead>
                            <tr>
                                <th>菜单编号</th>
                                <th>上级菜单</th>
                                <th>菜单名称</th>
                                <th>菜单图标</th>
                                <th>菜单标识</th>
                                <th>菜单地址</th>
                                <th>创建时间</th>
                                <th>状态</th>
                                <th>操作</th>
                            </tr>
                            </thead>

                            <tbody id="loadData">

                            </tbody>
                        </table>
                        <!--#include file="/admin/include/page.html"-->
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="menuAddModal" tabindex="-1" role="dialog" aria-labelledby="menuAddModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">添加菜单</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" role="form" id="menuForm">
                    <input type="hidden" id="menuId">
                    <div class="form-group">
                        <label for="parentMenu" class="col-sm-2 control-label">上级菜单</label>
                        <div class="col-sm-9">
                            <select id="parentMenu" class="form-control">

                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="menuName" class="col-sm-2 control-label">菜单名称</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" id="menuName" placeholder="请输入菜单名称">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="menuIcon" class="col-sm-2 control-label">菜单图标</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" id="menuIcon" placeholder="icon-double-angle-right" value="icon-double-angle-right">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="menuLabel" class="col-sm-2 control-label">菜单标识</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" id="menuLabel" placeholder="userList">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="menuUrl" class="col-sm-2 control-label">菜单地址</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" id="menuUrl" placeholder="/user/userList">
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" onclick="addMenu()">提交更改</button>
            </div>
        </div>
    </div>
</div>


<script>
    $("#btn-load").on("click", function () {
        var a = $(this);
        a.button("loading");
        $("#cupage").val("1");
        loadData();
    });

    function loadData() {
        var name = $("#name").val();
        var para = {
            curPage: $("#cupage").val(),
            name: name
        };
        loadGetServer($("#formurl").val(), para, function (data) {
            var $loadData = $("#loadData");
            $loadData.html("");
            var list = data.page.list;
            var html_ = "";
            for (var i = 0; i < list.length; i++) {
                html_ += "<tr><td>" + list[i].id + "</td><td>" + list[i].parentMenuName + "</td><td>" + list[i].name + "</td>";
                html_ += "<td>" + list[i].icon + "</td><td>" + list[i].label + "</td><td>" + replaceNull(list[i].url,'') + "</td><td>" + list[i].create_date + "</td>";
                if(list[i].status===1){
                    html_ += "<td><span class='label label-sm label-success'>正常</span></td>";
                }else{
                    html_ += "<td><span class='label label-sm label-warning'>异常</span></td>";
                }
                html_ += "<td><div class='hidden-sm hidden-xs btn-group'>";
                if(list[i].parent_id == 1){
                    html_ += "<button class='btn btn-xs btn-success' title='新增下级菜单' onclick='addChildMenu(" + list[i].id + ")'><i class='icon-plus bigger-120'></i>新增下级菜单</button>";
                } else{
                    html_ += "<button class='btn btn-xs btn-default'><i class='icon-plus bigger-120'></i>新增下级菜单</button>";
                }
                html_ += "<button class='btn btn-xs btn-info' title='修改菜单' onclick='editMenu(" + list[i].id + ")'><i class='icon-pencil bigger-120'></i>修改菜单</button>";
                html_ += "<button class='btn btn-xs btn-warning' title='删除菜单' onclick='showConfirm(" + list[i].id + ")'><i class='icon-trash bigger-120'></i>删除菜单</button>";
                html_ += "</div></td></tr>";
            }
            $loadData.html(html_);
            pageValue(data);
            pageData();
            $("#btn-load").button("reset");
        });
    }

    loadData();

    $('#menuAddModal').on('show.bs.modal', function () {
        $("#menuForm")[0].reset();
        loadGetServer('/manager/menu/getFirstMenu', {}, function (data) {
            var $parentMenu = $("#parentMenu");
            $parentMenu.html("");
            var list = data.list;
            var html = "";
            html += "<option value='1'>首页</option>";
            for (var i = 0; i < list.length; i++) {
                html += "<option value='" + list[i].id + "'>" + list[i].name + "</option>";
            }
            $parentMenu.html(html);
        });
    })

    function addMenu(){
        var para = {
            parentMenu: $("#parentMenu").val(),
            menuId: $("#menuId").val(),
            menuName: $("#menuName").val(),
            menuIcon: $("#menuIcon").val(),
            menuLabel: $("#menuLabel").val(),
            menuUrl: $("#menuUrl").val()
        };
        loadGetServer('/manager/menu/addMenu', para, function (data) {
            if(data.code != 0){
                alert(data.msg);
            }else{
                alert('操作成功')
                $('#menuAddModal').modal('hide');
            }
            loadData();
        });
    }

    function editMenu(menuId){
        var para = {
            id: menuId
        };
        loadGetServer('/manager/menu/getMenu', para, function (data) {
            if(data.code != 0){
                alert(data.msg);
            }else{
                $('#menuAddModal').modal('show');
                $("#myModalLabel").html("编辑菜单");
                $("#parentMenu").val(data.data.parent_id);
                $("#menuName").val(data.data.name);
                $("#menuIcon").val(data.data.icon);
                $("#menuLabel").val(data.data.label);
                $("#menuUrl").val(data.data.url);
                $("#menuId").val(data.data.id);
            }
            loadData();
        });
    }

    function deleteMenu(menuId){
        var para = {
            id: menuId
        };
        loadGetServer('/manager/menu/deleteMenu', para, function (data) {
            if(data.code != 0){
                alert(data.msg);
            }else{
                alert('操作成功')
            }
            loadData();
        });
    }

    function showConfirm(menuId) {
        var r = confirm("是否确定删除此菜单？");
        if (r == true) {
            deleteMenu(menuId)
        }
    }

    function addChildMenu(menuId){
        $('#menuAddModal').modal('show');
        $("#parentMenu").val(menuId);
    }
</script>